package com.simen.downloader;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

import java.security.cert.CertPathBuilder;

/**
 * Created by zhangming on 16/4/26.
 */
public final class DownloadConfiguration {
    final int threadPoolSize;
    final DownloadOptions defaultOptions;
    final DownloadCache downloadCache;
    final boolean openlist;

    protected DownloadConfiguration(Builder builder) {
        this.defaultOptions = builder.defaultOptions;
        this.downloadCache = builder.downloadCache;
        this.threadPoolSize = builder.threadPoolSize;
        this.openlist = builder.openlist;
    }

    public final static class Builder {
        private Context context;
        private String defaultFileDir;
        private String fileDir;
        private DownloadOptions defaultOptions;
        private DownloadCache downloadCache;
        private int threadPoolSize = 5;
        private long diskFileSize = 0;
        private int diskFileCount = 0;
        private boolean openlist = false;

        public Builder(Context context) {
            this.context = context.getApplicationContext();
            this.defaultFileDir = this.context.getExternalFilesDir(Environment
                    .DIRECTORY_DOWNLOADS).getPath();
        }

        public Builder setFileDir(String fileDir) {
            this.fileDir = fileDir;
            return this;
        }

        public Builder setDefaultOptions(DownloadOptions defaultOptions) {
            this.defaultOptions = defaultOptions;
            return this;
        }

        public Builder setDiskFileSize(long diskFileSize) {
            if (diskFileSize < 0) {
                return this;
            }
            this.diskFileSize = diskFileSize;
            return this;
        }

        public Builder setDiskFileCount(int diskFileCount) {
            if (diskFileCount < 0) {
                return this;
            }
            this.diskFileCount = diskFileCount;
            return this;
        }

        /**
         * @param threadPoolSize
         */
        public Builder setThreadPoolSize(int threadPoolSize) {
            this.threadPoolSize = threadPoolSize;
            return this;
        }

        public DownloadConfiguration build() {
            CheckConfig();
            return new DownloadConfiguration(this);
        }

        private void CheckConfig() {
            if (TextUtils.isEmpty(fileDir)) {
                this.fileDir = this.defaultFileDir;
            }

            if (this.defaultOptions == null) {
                this.defaultOptions = new DownloadOptions.Builder().setFileDir(this
                        .defaultFileDir).build();
            }

            if (downloadCache == null) {
                downloadCache = new DownloadCache(this.fileDir, diskFileSize, diskFileCount);
            }
        }


        public Builder requirelist(boolean openlist) {
            this.openlist = openlist;
            return this;
        }
    }
}
